package JFrame.Admin.MailManager;

import JFrame.*;
import entity.Mail;


import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import Dao.MailDaoImpl;

public class AddMailJFrame extends JFrame {
    JFrame jFrame =this;
    MailDaoImpl operation =new MailDaoImpl();

    public AddMailJFrame() {
        //页面大小不可变
        setResizable(false);
        initializing();
    }
    public void initializing(){

        JPanel panel=new JPanel();
        panel.setLayout(new GridLayout(7,2,10,10));
        panel.setBorder(new EmptyBorder(20, 20, 20, 20));
        add(panel);
        panel.setBackground(new Color(255, 255, 255));
        JLabel l_id =new JLabel("快件id:",SwingConstants.CENTER);
        JLabel l_uid =new JLabel("收件人id:",SwingConstants.CENTER);
        JLabel l_mailName =new JLabel("快件名称:",SwingConstants.CENTER);
        JLabel l_arrivalTime =new JLabel("快件到站时间:",SwingConstants.CENTER);
        JLabel l_mailState =new JLabel("派送情况:",SwingConstants.CENTER);
        JLabel l_completionTime =new JLabel("派送完成时间:",SwingConstants.CENTER);
        JTextField t_id =new JTextField(SwingConstants.CENTER);
        JTextField t_uid =new JTextField(SwingConstants.CENTER);
        JTextField t_mailName =new JTextField(SwingConstants.CENTER);
        JTextField t_arrivalTime =new JTextField(SwingConstants.CENTER);
        JTextField t_mailState =new JTextField(SwingConstants.CENTER);
        JTextField t_completionTime =new JTextField(SwingConstants.CENTER);
        JButton Yes =new JButton("确认");
        JButton Cancel =new JButton("取消");
        Yes.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (t_mailState.getText().equals("派送中") || t_mailState.getText().equals("未派送") ||
                        t_mailState.getText().equals("已派送")){
                    operation.Insert(new Mail(t_id.getText(),t_uid.getText(),t_mailName.getText(),
                            t_arrivalTime.getText(),t_completionTime.getText(),t_mailState.getText()));
                }else {
                    JOptionPane.showMessageDialog(jFrame,
                            "请输入正确运送状态，快件状态的格式有3种: 已派送，未派送，派送中", "！！！",JOptionPane.WARNING_MESSAGE);
                }

            }
        });
        Cancel.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jFrame.dispose();
            }
        });
        Cancel.setBackground(new Color(102,204,255));
        Yes.setBackground(new Color(102,204,255));

        setSize(500,700);
        setTitle("添加快件——管理员");
        setResizable(false);
        setLocationRelativeTo(getOwner());
        Tools.setFonts(20,1,l_id, l_uid,l_mailName,l_arrivalTime,l_mailState,l_completionTime,Yes,Cancel);
        Tools.setFonts(20,0,t_id, t_uid,t_mailName,t_arrivalTime,t_mailState,t_completionTime);
        Tools.setTextFieldCenter(t_id, t_uid,t_mailName,t_arrivalTime,t_mailState,t_completionTime);
        Tools.addComponents(panel,l_id,t_id, l_uid, t_uid,l_mailName,t_mailName,l_arrivalTime,t_arrivalTime,l_mailState,t_mailState,l_completionTime,t_completionTime,Yes,Cancel);
        setVisible(true);
    }

}
